//上传单个文件
// uploadFile:function(opts){
//     console.log(opts);
//     //thumbTempFilePath
//     //tempFilePath
//   wx.uploadFile({
//       filePath: opts.thumbTempFilePath,
//       name: 'logo',
//       formData:{
//         user_token:wx.getStorageSync('token'),
//       },
//       url: config.host+config.upload_img,
//       success :function(rsa){
//         console.log(rsa);
//       },
//       fail:function(){},
//       complete:function(rs){
//           console.log(rs);
//       },
//     })
// },

//上传多个文件的方式进行这个
// uploadFiles  : function(opts){
//   this.uploadFile(opts.files[0]);
//   console.log(opts);
// }
import {config} from './config.js';
//上传多个文件的那种插件，一个文件也包含视频跟图片
var kuploader = function(opts){
    var ths = this;
    this.files = opts.files;
    this.index = 0,//当前索引ID
    this.bufferlist = [];
    //输出的一个buffer
    this.buffer = function(){
        this.type = '';
        this.thumb_url = '';//缩略图路径
        this.res_url = '';//资源路径
    },
    //上传视屏文件
    this.uploadVideo = function(file,callback){
        //上传视屏缩略图文件
        var buffer = new this.buffer();
        buffer.type = 'video';
        var uploadThumb = function(callback){
            wx.uploadFile({
                filePath: file.thumbTempFilePath,//缩略图路径
                name:'video_thumb',
                formData:{
                    user_token:wx.getStorageSync('token'),
                },
                url: config.host+config.upload_img,
                success :function(rsa){
                    var data = rsa.data;
                    data = JSON.parse(data);
                    buffer.thumb_url = data.data.url;
                },
                complete:function(){
                    callback();
                },
            })
        };
        var uploadBody = function(callback){
            wx.uploadFile({
                filePath: file.tempFilePath,//缩略图路径
                name:'video',
                formData:{
                    user_token:wx.getStorageSync('token'),
                },
                url: config.host+config.upload_video,
                success :function(rs){
                    var data = rs.data;
                    data = JSON.parse(data);
                    if(data.status==1){
                        buffer.res_url = data.data.url;
                    }else{
                        wx.showModal({
                            content:data.message,
                            cancelColor: 'cancelColor',
                        })
                    }
                },
                complete:function(){
                    callback();
                },
            })
        };
        //视屏文件路径：tempFilePath
        uploadThumb(function(){
            uploadBody(function(){
                callback(buffer);
            });
        });
    }
    //上传图片
    this.uploadImage = function(file,callback){
        var buffer = new this.buffer();
        buffer.type = 'image';
        var uploadBody = function(callback){
            wx.uploadFile({
                filePath: file.tempFilePath,//缩略图路径
                name:'image',
                formData:{
                    user_token:wx.getStorageSync('token'),
                },
                url: config.host+config.upload_img,
                success :function(rs){
                    var data = rs.data;
                    data = JSON.parse(data);
                    if(data.status==1){
                        buffer.res_url = data.data.url;
                        buffer.thumb_url=data.data.url;
                    }else{
                        wx.showModal({
                            content:data.message,
                            cancelColor: 'cancelColor',
                        })
                    }
                },
                complete:function(){
                    callback();
                },
            })
        };
        //图片文件路径：tempFilePath
        uploadBody(function(){
            callback(buffer);
        });
    }

    //上传文件
    this.uploadFile = function(file,callback){
        if(file.fileType=='video'){
            this.uploadVideo(file,function(buffer){
                ths.bufferlist.push(buffer);
                callback();
            });//上传视屏文件
        }else if(file.fileType=='image'){
            //这个是上传图片的相关部分
            this.uploadImage(file,function(buffer){
                ths.bufferlist.push(buffer);
                callback();
            });
        }else{
            //todo--
            //这个是上传图片的相关部分
            this.uploadImage(file,function(buffer){
                ths.bufferlist.push(buffer);
                callback();
            });
        }
    }
    this.uploadIndex = function(_callback){
        if(this.files[this.index]==undefined){
            ths.uploadFinish(ths.bufferlist);
            return;
        }
        this.uploadFile(this.files[this.index],
            function(rs){
                //执行成功
                ths.index++;
                ths.uploadIndex();
            }   
        );
    },

    //上传完成之后的事件监听
    this.uploadFinish = function(){
        this.onUploadFinish&&this.onUploadFinish(this.bufferlist);
    };

    //执行上传所有文件的方法操作处理
    this.uploadAll = function(callback){
        this.uploadIndex();//上传当前序号的一个文件
    }
}
module.exports = {
    kuploader
}